package demo;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SaxDemo {

    public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {
        xmlTest();
    }

    public static void xmlTest() throws ParserConfigurationException, SAXException, IOException {
        //创建一个DOM解析器工厂对象;
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        //通过工厂对象创建DOM解析器
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        //解析文档
//        InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("com\\first\\person.xml");
        InputStream inputStream = new FileInputStream(new File("./config/SRCClassPatch.xml"));
        Document d = documentBuilder.parse(inputStream);//doc 即为树状结构的存储对象,在内存中;

        List<Map<String, Object>> srcList = new ArrayList<>();

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
//            Document d=db.parse("books.xml");
            NodeList nl = d.getElementsByTagName("classpathentry");
            System.out.println("集合节点的个数:" + nl.getLength());
            for (int i = 0; i < nl.getLength(); i++) {
                Map<String, Object> map = new HashMap<>();
                Node book = nl.item(i);
                NamedNodeMap attrs = book.getAttributes();
                System.out.println("节点属性的个数：" + attrs.getLength());
                for (int j = 0; j < attrs.getLength(); j++) {
                    Node attr = attrs.item(j);
//                    attr.getAttributes().getNamedItem("kind").getNodeValue()
//                    attr.getAttributes().getNamedItem("kind").getNodeValue()
//                    System.out.println("属性名：" + attr.getNodeName());
//                    System.out.println("属性值：" + attr.getNodeValue());
                    map.put(attr.getNodeName(), attr.getNodeValue());
                }
                srcList.add(map);
            }
            srcList.forEach((p) -> System.out.println(p));
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


//        NodeList XMLpersonlist = doc.getElementsByTagName("classpath");
//        for (int i = 0; i <XMLpersonlist.getLength(); i++) {
//            Map<String, Object> map = new HashMap<>();
//            //获取属性值
//            String p_id = XMLpersonlist.item(i).getAttributes().getNamedItem("kind").getNodeValue();
//            map.put("p_id", p_id);
//            NodeList childNodes = XMLpersonlist.item(i).getChildNodes();
//            for (int j = 0; j < childNodes.getLength(); j++) {
//                Node children_node = childNodes.item(j);
//                String node_name = children_node.getNodeName();
//                if ("name".equals(node_name)){
//                    //把文本也看成一个节点，<name>zy</name> 中zy当做节点；children_node.getFirstChild()表示获取zy这个节点；
//                    //zy.getNodeValue() 获取的值就是zy;
//                    String p_name = children_node.getFirstChild().getNodeValue();
//                    System.out.println(p_name);
//                }else if ("age".equals(node_name)){
//                    String p_age = children_node.getFirstChild().getNodeValue();
//                    System.out.println(p_age);
//                }
//            }
//            srcList.add(map);
//        }
//        srcList.forEach((p) -> System.out.println(p));
    }
}
